// Copyright 2010, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v200909;

import com.google.api.adwords.lib.AdWordsService;
import com.google.api.adwords.lib.AdWordsServiceLogger;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.lib.utils.MapUtils;
import com.google.api.adwords.v200909.cm.Paging;
import com.google.api.adwords.v200909.cm.Placement;
import com.google.api.adwords.v200909.o.Attribute;
import com.google.api.adwords.v200909.o.AttributeType;
import com.google.api.adwords.v200909.o.IdeaType;
import com.google.api.adwords.v200909.o.PlacementAttribute;
import com.google.api.adwords.v200909.o.PlacementTypeAttribute;
import com.google.api.adwords.v200909.o.RelatedToUrlSearchParameter;
import com.google.api.adwords.v200909.o.RequestType;
import com.google.api.adwords.v200909.o.SearchParameter;
import com.google.api.adwords.v200909.o.SiteConstantsPlacementType;
import com.google.api.adwords.v200909.o.TargetingIdea;
import com.google.api.adwords.v200909.o.TargetingIdeaPage;
import com.google.api.adwords.v200909.o.TargetingIdeaSelector;
import com.google.api.adwords.v200909.o.TargetingIdeaServiceInterface;

import java.util.Map;

/**
 * This example gets related placements.
 *
 * Tags: TargetingIdeaService.get
 *
 * @author api.arogal@gmail.com (Adam Rogal)
 */
public class GetRelatedPlacements {
  public static void main(String[] args) throws Exception {
    // Log SOAP XML request and response.
    AdWordsServiceLogger.log();

    // Get AdWordsUser from "~/adwords.properties".
    AdWordsUser user = new AdWordsUser();

    // Get the TargetingIdeaService.
    TargetingIdeaServiceInterface targetingIdeaService =
        user.getService(AdWordsService.V200909.TARGETING_IDEA_SERVICE);

    // Create seed URL.
    String url = "mars.google.com";

    // Create selector.
    TargetingIdeaSelector selector = new TargetingIdeaSelector();
    selector.setRequestType(RequestType.IDEAS);
    selector.setIdeaType(IdeaType.PLACEMENT);
    selector.setRequestedAttributeTypes(
        new AttributeType[] {AttributeType.PLACEMENT, AttributeType.PLACEMENT_TYPE});

    // Set selector paging (required for targeting idea serivce).
    Paging paging = new Paging();
    paging.setStartIndex(0);
    paging.setNumberResults(10);
    selector.setPaging(paging);

    // Create related to url search parameter.
    RelatedToUrlSearchParameter relatedToUrlSearchParameter =
        new RelatedToUrlSearchParameter();
    relatedToUrlSearchParameter.setUrls(new String[] {url});
    relatedToUrlSearchParameter.setIncludeSubUrls(false);
    selector.setSearchParameters(new SearchParameter[] {relatedToUrlSearchParameter});

    // Get related placements.
    TargetingIdeaPage page = targetingIdeaService.get(selector);

    // Display related placements.
    if (page.getEntries() != null && page.getEntries().length > 0) {
      for (TargetingIdea targetingIdea : page.getEntries()) {
        Map<AttributeType, Attribute> data = MapUtils.toMap(targetingIdea.getData());        
        Placement placement = ((PlacementAttribute) data.get(AttributeType.PLACEMENT)).getValue();
        SiteConstantsPlacementType placementType =
            ((PlacementTypeAttribute) data.get(AttributeType.PLACEMENT_TYPE)).getValue();
        System.out.println("Placement with url '" + placement.getUrl() + "' and type '"
            + placementType.toString() + "' was found.");
      }
    } else {
      System.out.println("No related placements were found.");
    }
  }
}
